
//判断时间在几分几秒前
function getDateDiff(dateStr) {
	let publishTime = getDateTimeStamp(dateStr) / 1000,
	d_seconds,
	d_minutes,
	d_hours,
	d_days,
	timeNow = parseInt(new Date().getTime() / 1000),
	d,

	date = new Date(publishTime * 1000),
	Y = date.getFullYear(),
	M = date.getMonth() + 1,
	D = date.getDate(),
	H = date.getHours(),
	m = date.getMinutes(),
	s = date.getSeconds();
	//小于10的在前面补0
	if (M < 10) {
		M = '0' + M;
	}
	if (D < 10) {
		D = '0' + D;
	}
	if (H < 10) {
		H = '0' + H;
	}
	if (m < 10) {
		m = '0' + m;
	}
	if (s < 10) {
		s = '0' + s;
	}

	d = timeNow - publishTime;
	d_days = parseInt(d / 86400);
	d_hours = parseInt(d / 3600);
	d_minutes = parseInt(d / 60);
	d_seconds = parseInt(d);

	if (d_days > 0 && d_days < 3) {
		return d_days + '天前';
	} else if (d_days <= 0 && d_hours > 0) {
		return d_hours + '小时前';
	} else if (d_hours <= 0 && d_minutes > 0) {
		return d_minutes + '分钟前';
	} else if (d_seconds < 60) {
		if (d_seconds <= 0) {
				return '刚刚';
		} else {
				return d_seconds + '秒前';
		}
	} else if (d_days >= 3 && d_days < 30) {
		return M + '-' + D + ' ' + H + ':' + m;
	} else if (d_days >= 30) {
		return Y + '-' + M + '-' + D + ' ' + H + ':' + m;
	}
}

function getDateTimeStamp(dateStr) {
	// 如果时间格式为2020/07/09 21:43:19.000  需要去掉.000 不然ios和firefox会有问题
	return Date.parse(dateStr.replace(/-/gi, "/"));
}　　

//比较时间大小
function compareDate(t1,t2) {
	var date = new Date();
	var a = t1.split(":");
	var b = t2.split(":");
	return date.setHours(a[0],a[1]) < date.setHours(b[0],b[1]);
}

//获取当天日期，格式：2021-08-01
function getCurrDate() {
	let date = new Date();
	let year = date.getFullYear();
	let month = date.getMonth()+1 >= 10 ? date.getMonth()+1 : '0'+(date.getMonth()+1); 
	let day = date.getDate() >= 10 ? date.getDate() : '0'+date.getDate();
	let dateStr = year + '-'+ month + '-' + day;
	return dateStr;
}

//获取当天往后6天共7天时间的日期和星期
function getWeek(dayNum) {
	let now = new Date(); 
	let nowTime = now.getTime(); 
	let oneDayTime = 24*60*60*1000;
	let dateList = [];
	for(let i = 0 ; i < dayNum ; i++){ 
		//显示周一
		let ShowTime = nowTime + i*oneDayTime ; 
		//初始化日期时间
		let myDate = new Date(ShowTime);
		let year = myDate.getFullYear();    
		let month = myDate.getMonth()+1 >= 10 ? myDate.getMonth()+1 : '0'+(myDate.getMonth()+1);  
		let date = myDate.getDate() >= 10 ? myDate.getDate() : '0'+myDate.getDate(); 
		// console.log(year+"-"+month+"-"+date)
		let str = "周" + "日一二三四五六".charAt(myDate.getDay());
		// console.log(str)
		let dateObj = {
			date: month+"-"+date,
			week: str
		}
		dateList.push(dateObj);
	}
	return dateList;
}

function getCurrentWeek(week = 0) {
	const currentDate = new Date();
	
	  const currentDay = currentDate.getDay();
	
	  const startOfWeek = new Date(currentDate);
	
	  startOfWeek.setDate(currentDate.getDate() - currentDay + (currentDay === 0 ? -6 : 1) + (week * 7));
	
	  const weekDays = [];
	
	  for (let i = 0; i < 7; i++) {
	
	    const day = new Date(startOfWeek);
	
	    day.setDate(startOfWeek.getDate() + i);
	
	    weekDays.push({ date: (day.getMonth()+1) + '-' + day.getDate(), week: '周' + ['日', '一', '二', '三', '四', '五', '六'][day.getDay()] });
		
	  }
	
	  return weekDays;
}

//获取链接参数
function getUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
	var r = window.location.search.substr(1).match(reg); 
	if (r != null) return unescape(r[2]); return null; 
}

//封装请求方法
let baseUrl = 'http://zhjx.zjhu.edu.cn';
// #ifdef H5
// baseUrl = '/api'
// #endif
function requestFun(url,data={},method='GET') {
	let _this = this;
	return new Promise((resolve, reject) => {
		uni.showLoading({title:'加载中',mask: true})
		uni.request({
			url: baseUrl + url,
			method: method,
			data: data,
			header: {
				'content-type': 'application/json'
			},
			success: (res) => {
				uni.hideLoading();
				if(res.statusCode == 200) {
					if(res.data.code == "10000") {
						resolve(res)
					}else if(res.data.code == "99999") {
						// uni.clearStorage();
						// #ifdef MP-WEIXIN
							uni.showToast({ title: '请登录', icon: 'none' ,success() {
								setTimeout(()=> {
									uni.navigateTo({
										url: '/pages/login/login',
									});
								},1000)
							}})
						// #endif
							
						// #ifdef H5
							setTimeout(()=> {
								if(data.orderId && data.orderId != undefined) {
									uni.setStorage({
										key: 'orderId',
										data: data.orderId
									});
								}
							},2000)
							uni.showToast({ title: '请登录', icon: 'none' ,success() {
								setTimeout(()=> {
									 // window.location.href = "http://wxtest.lib.hunnu.edu.cn/api/hunnu/gsroom.aspx";
									uni.navigateTo({
										url: '/pages/authorizeLogin'
									})
								},1000)
							}})
						// #endif	
						reject(res);
					}else {
						uni.showToast({
							title: res.data.msg == null ? '系统繁忙，请稍后再试' : res.data.msg,
							icon: 'none'
						})
						reject(res);
					}
					
				}else {
					uni.showToast({
						title: res.data.msg == null ? '系统繁忙，请稍后再试' : res.data.msg,
							icon: 'none'
					})
					reject(res);
				}
			},
			fail: (res) => {
				uni.hideLoading();
				uni.showToast({
					title: "系统繁忙，请稍后再试",
					icon: 'none'
				})
				reject(res);
			}
		})
	})
}

//把所有方法导出暴露出去
module.exports = {
	getDateDiff: getDateDiff,
	requestFun: requestFun,
	getCurrDate: getCurrDate,
	getWeek: getWeek,
	compareDate: compareDate,
	getUrlParam: getUrlParam,
	getCurrentWeek: getCurrentWeek
}